using System;
using System.Collections.Generic;
using System.Linq;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;

namespace testDB
{
    public class UserRepository : IUserRepository, IDisposable
    {
        private readonly IObjectContainer objectContainer;

        public UserRepository(IObjectContainer ObjectContainer)
        {
            objectContainer = ObjectContainer;
        }

        #region IDisposable Members

        public void Dispose()
        {
            objectContainer.Close();
            objectContainer.Dispose();
        }

        #endregion

        #region IUserRepository Members

        public void Save(User user)
        {
            
            objectContainer.Store(user);
            
        }

        public User Get(string ID)
        {
            return GetByUsername(ID);
        }

        public IEnumerable<User> Query()
        {
            return objectContainer.Cast<User>();
        }


        public void Delete(User user)
        {
            throw new NotImplementedException();
        }


        public User GetByUsername(string username)
        {
            IObjectSet set = objectContainer.QueryByExample(new User() {Username = username});
            if (set.Count >= 1)
                return set[0] as User;
            else
                return null;
        }

        #endregion
    }
}